package com.dyx.api.files.conf;


import com.dyx.spring.boot.zookeeper.service.BackendService;
import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;



@Configuration
public class SolrProxyServletConfiguration implements EnvironmentAware {

    @Autowired
    private BackendService backendService;

    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), "/upload/*");
//        servletRegistrationBean.addInitParameter("targetUri", propertyResolver.getProperty("target_url"));
//        servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, propertyResolver.getProperty("logging_enabled", "false"));
//        return servletRegistrationBean;
//        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), "/upload/**");


//        String remotePath = ("ms-api-files" + "/files/getFileInfo").replace("/", "@");
//        String path = backendService.getRequestLatestVersionNode(remotePath);
//        System.out.println(path);
//        servletRegistrationBean.addInitParameter("targetUri", "http://"+path+"/ms-api-files/upload");
//        servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, propertyResolver.getProperty("logging_enabled", "true"));
        return servletRegistrationBean;
    }

    private RelaxedPropertyResolver propertyResolver;

    @Override
    public void setEnvironment(Environment environment) {
        this.propertyResolver = new RelaxedPropertyResolver(environment, "proxy.solr.");
    }
}